package cn.agiledata.bank.linkbank.impl.ccb;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayResponse;
import cn.agiledata.bank.linkbank.logic.DigitalSignValidatable;

/**
 * 中国建设银行网上银行支付响应
 * 
 * @author 马贤波
 * 
 */
public final class CcbNetPayResponse extends AbstractBankPayResponse
        implements DigitalSignValidatable {
    static final String[] RESPONSE_KEYS = { CcbNetConstant.POS_ID,
            CcbNetConstant.BRANCH_ID, CcbNetConstant.ORDER_ID,
            CcbNetConstant.AMOUNT, CcbNetConstant.CURRENCY_TYPE,
            CcbNetConstant.REMARK1, CcbNetConstant.REMARK2,
            CcbNetConstant.RETURN_CODE };

    private String certFilePath;

    public void setCertFilePath(String certFilePath) {
        this.certFilePath = certFilePath;
    }

    protected AbstractBankPayResponse getCopiedInstance() {
        CcbNetPayResponse response = new CcbNetPayResponse();
        response.certFilePath = this.certFilePath;
        return response;
    }

    protected String getSuccessReturnCode() {
        return CcbNetConstant.RETURN_CODE_SUCCESS;
    }

    protected String[] getValidKeys() {
        return RESPONSE_KEYS;
    }

    protected boolean isValidKey(String key) {
        if (CcbNetConstant.DIGITAL_SIGN.equals(key)) {
            return true;
        }
        return super.isValidKey(key);
    }

    public BigDecimal getAmount() {
        try {
            return new BigDecimal(getMessage(CcbNetConstant.AMOUNT));
        } catch (Exception e) {
            return null;
        }
    }

    public String getOrderId() {
        return getMessage(CcbNetConstant.ORDER_ID);
    }

    public String getReturnCode() {
        return getMessage(CcbNetConstant.RETURN_CODE);
    }

    /**
     * @return 商户柜台代码
     */
    public String getPosId() {
        return getMessage(CcbNetConstant.POS_ID);
    }

    /**
     * @return 分行代码
     */
    public String getBranchId() {
        return getMessage(CcbNetConstant.BRANCH_ID);
    }

    public String getDigitalSign() {
        return getMessage(CcbNetConstant.DIGITAL_SIGN);
    }

    public boolean validateDigitalSign(String cryptoKey)
            throws LinkBankException {
        Map values = new HashMap();
        String[] keys = RESPONSE_KEYS;
        for (int i = 0; i < keys.length; i++) {
            values.put(keys[i], getMessage(keys[i]));
        }
        return CcbUtils.validateDigitalSign(values, keys,
                getDigitalSign(), this.certFilePath);
    }

    public String getCryptoKeyDescription() {
        return null;
    }

    public boolean validateCryptoKey(String cryptoKey) {
        return true;
    }

}
